<?php
use Workerman\Worker;
use Workerman\WebServer;
use Workerman\Lib\Timer;
use PHPSocketIO\SocketIO;

include __DIR__ . '../../vendor/autoload.php';
include __DIR__ . '../../vendor/RedisDriver.php';

$total = 1;
$Redis = new RedisDriver();
$Redis->connect();

$worker = new Worker('http://0.0.0.0:6161');

// 进程启动时 连接redis
$worker->onWorkerStart = function() {
    echo "Worker starting...\n";
};

$worker->onConnect = function($connection)
{
    echo "new connection from ip " . $connection->getRemoteIp() . "\n";
};

$worker->onClose = function($connection)
{
    echo "connection closed\n";
};

//发送消息
$worker->onMessage = function($connection, $data) {

    global $Redis,$total;
    $total= $total+1;
    $connection->send('发送成功：'.'当前信息数'.$total);
    $Redis->setStr('current-message-total',$total);
};
$worker->listen();

Worker::runAll();